﻿Imports DevExpress.XtraEditors

Public Class frmThiCong_CapNhat
    Dim Me_AddEdit As String
    Dim Me_Dtb_SoTc As New DataTable
    Dim Me_Dtb As New DataTable

#Region "Proc"

    Private Sub InitControl()
        Me.txtSoTc.Properties.AppearanceFocused.BackColor = Project_FocusedColor
        Me.datNgayLap.Properties.AppearanceFocused.BackColor = Project_FocusedColor
        Me.lueMaDv.Properties.AppearanceFocused.BackColor = Project_FocusedColor
    End Sub

    Private Sub InitLookup_DonVi()
        Dim myDtb As New DataTable

        Me.lueMaDv.Properties.Columns.AddRange(New DevExpress.XtraEditors.Controls.LookUpColumnInfo() {New DevExpress.XtraEditors.Controls.LookUpColumnInfo("madv", "Mã đơn vị", 30, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("tendv", "Tên đơn vị", 70, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Default), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("donvi", "", 85, DevExpress.Utils.FormatType.None, "", False, DevExpress.Utils.HorzAlignment.Default)})
        Me.lueMaDv.Properties.DisplayMember = "donvi"
        Me.lueMaDv.Properties.ValueMember = "madv"

        Dim myDvF As New DonViF
        myDtb = myDvF.GetAll
        Me.lueMaDv.Properties.DataSource = myDtb

    End Sub

    Private Sub InitGrid()
        Dim i As Integer
        With Me.GridView1
            i = 0
            .Columns(i).FieldName = "chon"
            .Columns(i).Caption = " "
            .Columns(i).Width = 20

            i = i + 1
            .Columns(i).FieldName = "sophieu"
            .Columns(i).Caption = "Số phiếu"
            .Columns(i).Width = 40

            i = i + 1
            .Columns(i).FieldName = "so_don"
            .Columns(i).Caption = "Số đơn"
            .Columns(i).Width = 50

            i = i + 1
            .Columns(i).FieldName = "khachhnag"
            .Columns(i).Caption = "Khách hàng"
            .Columns(i).Width = 80

            i = i + 1
            .Columns(i).FieldName = "diachi"
            .Columns(i).Caption = "Địa chỉ"
            .Columns(i).Width = 140

            For i = 1 To .Columns.Count - 1
                .Columns(i).OptionsColumn.AllowEdit = False
            Next

        End With

    End Sub

    Private Sub LoadGrid()
        Dim myTcF As New ThiCongF
        Me_Dtb = myTcF.GetDetail(Me.txtSoTc.EditValue.ToString.Trim)
        Me.GridControl1.DataSource = Me_Dtb

    End Sub

    Private Function GetNewSoTc() As String
        Dim s As String
        Dim myTcF As New ThiCongF
        Dim myDtb As New DataTable
        myDtb = myTcF.GetLastSoTc
        If myDtb.Rows.Count = 1 Then
            s = (CLng(myDtb.Rows(0).Item("sotc").ToString.Substring(2, 4)) + 1).ToString
            Dim i As Integer
            For i = 1 To 4 - s.Length
                s = "0" + s
            Next
            Return s
        Else
            Return "0001"
        End If

    End Function

    Private Sub AddNew()
        Me.txtSoTc.EditValue = Project_Nam.ToString.Substring(2, 2) + GetNewSoTc()
        Me.datNgayLap.EditValue = Now.Date
        Me.lueMaDv.EditValue = ""
        Me_AddEdit = "add"
        Status(1)

        LoadGrid()
    End Sub

    Private Sub LoadEdit()
        Dim myTcF As New ThiCongF
        Dim Dtb As New DataTable
        Dtb = myTcF.GetMaster(frmThiCong_CapNhat_SoTc)
        If Dtb.Rows.Count = 1 Then
            Me.txtSoTc.EditValue = Dtb.Rows(0).Item("sotc")
            Me.datNgayLap.EditValue = Dtb.Rows(0).Item("ngaylap")
            Me.lueMaDv.EditValue = Dtb.Rows(0).Item("madv")
        End If

        LoadGrid()
        Status(0)
        Me_AddEdit = "edit"
    End Sub

    Private Function CheckInput() As Boolean

        If Me.txtSoTc.EditValue.ToString.Length <> 6 Or Not IsNumeric(Me.txtSoTc.EditValue.ToString.Trim) Then
            XtraMessageBox.Show("Số hồ sơ không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Me.txtSoTc.Focus()
            Return False
            Exit Function
        End If

        If Me.datNgayLap.DateTime.Month <> Project_Thang Or Me.datNgayLap.DateTime.Year <> Project_Nam Then
            XtraMessageBox.Show("Ngày lập phải nằm trong tháng năm làm việc", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.datNgayLap.Focus()
            Return False
            Exit Function
        End If

        If Me.lueMaDv.EditValue = "" Then
            XtraMessageBox.Show("Đơn vị không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Me.lueMaDv.Focus()
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Sub Status(ByVal i As Integer)
        Select Case i
            Case 0 'xem
                Me.txtSoTc.Enabled = False
                Me.datNgayLap.Enabled = False
                Me.lueMaDv.Enabled = False

                Me.btnThemTc.Text = "Thêm hồ sơ"
                Me.btnSuaTc.Text = "Sửa"
                Me.btnSuaTc.Enabled = True
                Me.btnXoaTc.Enabled = True
                Me.btnXem.Enabled = True

                Me.btnThemCt.Enabled = False
                Me.btnXoaCt.Enabled = False

            Case 1 'them
                Me.txtSoTc.Enabled = True
                Me.datNgayLap.Enabled = True
                Me.lueMaDv.Enabled = True

                Me.btnThemTc.Text = "Ghi hồ sơ"
                Me.btnSuaTc.Text = "Bỏ qua"
                Me.btnThemTc.Enabled = False
                Me.btnSuaTc.Enabled = True
                Me.btnXoaTc.Enabled = False
                Me.btnXem.Enabled = False

                Me.btnThemCt.Enabled = True
                Me.btnXoaCt.Enabled = True

            Case 2 'sua
                Me.txtSoTc.Enabled = False
                Me.datNgayLap.Enabled = True
                Me.lueMaDv.Enabled = True

                Me.btnThemTc.Text = "Ghi hồ sơ"
                Me.btnSuaTc.Text = "Bỏ qua"
                Me.btnSuaTc.Enabled = True
                Me.btnXoaTc.Enabled = False
                Me.btnXem.Enabled = False

                Me.btnThemCt.Enabled = True
                Me.btnXoaCt.Enabled = True

        End Select
    End Sub

    Private Function CheckKey() As Boolean
        Dim myTcF As New ThiCongF
        If myTcF.ExistsSoTc(Me.txtSoTc.EditValue.ToString.Trim) Then
            XtraMessageBox.Show("Số hồ sơ đã tồn tại", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtSoTc.Focus()
            Return False
            Exit Function
        End If
        Return True
    End Function

    Private Sub AddDetail()
        'dua danh sach ma trong grid vao _dtb
        frmThiCong_CapNhat_Dtb = New System.Data.DataTable("MyDTB")
        frmThiCong_CapNhat_Dtb.Columns.Add("sophieu", GetType(String))
        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            frmThiCong_CapNhat_Dtb.Rows.Add(New Object() {Me.GridView1.GetDataRow(i).Item("sophieu")})
        Next

        'mo form tra cuu
        Project_Form = Me.Name
        Dim f As New frmChietTinh
        f.ShowDialog()

        'them chietinh
        Dim myChietTinhF As New ChietTinhF
        Dim myDtb As New DataTable
        For i = 0 To frmChietTinh_Dtb.Rows.Count - 1
            myDtb = myChietTinhF.GetWhereSoPhieu(frmChietTinh_Dtb.Rows(i).Item("sophieu"))
            If myDtb.Rows.Count = 0 Then Exit Sub

            Me_Dtb.Rows.Add()
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("chon") = False
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("sophieu") = myDtb.Rows(0).Item("sophieu")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("so_don") = myDtb.Rows(0).Item("so_don")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("khachhang") = myDtb.Rows(0).Item("khachhang")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("diachi") = myDtb.Rows(0).Item("diachi")
        Next

    End Sub

    Private Function Insert() As Boolean
        Dim myTc As New ThiCong
        Dim myTcF As New ThiCongF

        myTc.SoTc = Me.txtSoTc.EditValue.ToString.Trim
        myTc.NgayLap = Me.datNgayLap.EditValue
        myTc.MaDv = Me.lueMaDv.EditValue

        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            myTc.SoPhieu = Me.GridView1.GetRowCellValue(i, "sophieu")
            myTc.TrangThai = 0
            If Not myTcF.Insert(myTc) Then
                XtraMessageBox.Show("Lỗi tại dòng " + i.ToString, "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
                Exit Function
            End If
        Next

        Return True
    End Function

    Private Function Delete() As Boolean
        Dim myTcF As New ThiCongF

        If Not myTcF.Delete(Me.txtSoTc.EditValue.ToString.Trim) Then
            XtraMessageBox.Show("Lỗi xóa dữ liệu", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
            Exit Function
        End If

        Return True
    End Function

#End Region

    Private Sub frmThiCong_CapNhat_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If frmThiCong_CapNhat_AddEdit = "add" Then
            Me.txtSoTc.Focus()
        Else
            Me.datNgayLap.Focus()
        End If
    End Sub

    Private Sub frmThiCong_CapNhat_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        frmThiCong_CapNhat_Exists = False
    End Sub

    Private Sub frmThiCong_CapNhat_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitControl()
        InitLookup_DonVi()
        InitGrid()

        If frmThiCong_CapNhat_AddEdit = "add" Then
            AddNew()
        ElseIf frmThiCong_CapNhat_AddEdit = "edit" Then
            LoadEdit()
        End If

    End Sub

    Private Sub cmdThemTc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemTc.Click
        If Me.btnThemTc.Text = "Thêm hồ sơ" Then 'add new
            AddNew()
            Me.txtSoTc.Focus()

        ElseIf Me.btnThemTc.Text = "Ghi hồ sơ" Then 'save

            If Me_AddEdit = "add" Then
                If Not CheckKey() Then Exit Sub
                If Not CheckInput() Then Exit Sub
                If Not Insert() Then Exit Sub

            ElseIf Me_AddEdit = "edit" Then
                If Not CheckInput() Then Exit Sub
                If Not Delete() Then Exit Sub
                If Not Insert() Then Exit Sub

            End If

            XtraMessageBox.Show("Ghi dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Status(0)
        End If

    End Sub

    Private Sub cmdSuaChietTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuaTc.Click
        If Me.btnSuaTc.Text = "Sửa" Then
            Status(2)
            Me.GridControl1.Focus()

        ElseIf Me.btnSuaTc.Text = "Bỏ qua" Then
            Status(0)

        End If
    End Sub

    Private Sub cmdThemCt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemCt.Click
        AddDetail()
    End Sub

    Private Sub cmdXoaCt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaCt.Click
        Dim i As Integer
        Do While i <= Me.GridView1.DataRowCount - 1
            If Me.GridView1.GetRowCellValue(i, "chon") = True Then
                Me.GridView1.DeleteRow(i)
            Else
                i = i + 1
            End If
        Loop
    End Sub

    Private Sub cmdXoaTc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaTc.Click
        If XtraMessageBox.Show("Chắc chắn xóa hồ sơ?", "Quản lý chiết tính", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim myTcF As New DuToanF
            If myTcF.Delete(Me.txtSoTc.EditValue.ToString.Trim) Then
                XtraMessageBox.Show("Xóa dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.Close()
            Else
                XtraMessageBox.Show("Xóa dữ liệu không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If
    End Sub

#Region "Focus"

    Private Sub txtSoTc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSoTc.KeyDown
        If e.KeyCode = Keys.Enter Then Me.datNgayLap.Focus()
    End Sub

    Private Sub datNgayLap_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles datNgayLap.KeyDown
        If e.KeyCode = Keys.Enter Then Me.lueMaDv.Focus()
    End Sub

    Private Sub lueMaDv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lueMaDv.KeyDown
        If e.KeyCode = Keys.Enter Then Me.btnThemCt.Focus()
    End Sub

#End Region

    Private Sub GridView1_RowCountChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.RowCountChanged
        If Me.GridView1.DataRowCount = 0 Then
            Me.btnThemTc.Enabled = False
        Else
            Me.btnThemTc.Enabled = True
        End If
    End Sub

End Class